/* Copyright (C) 2012  	Mathieu Marleix <mathieu.marleix@aerian.fr>
 * 						Sébastien Dufay
 * 						Romain Noel
 * 						Nicolas Dennewald
 * 						Slim Aloui
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.esiee.mobiliteam.data;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Vector;
/**
 * Classe visant à trier les items obtenus via
 * le flux RSS, les stocker dans une liste tout en associant
 * chacune des catégories à l'elements en question.
 * @author Marleix Mathieu
 * @author Dufay Sébastien
 * @author Romain Noel
 * @author Nicolas Dennewald
 * @author Slim Aloui
 */
public class RSSFeed implements Serializable{

	private static final long serialVersionUID = 4721439046446069070L;
	private String title = null;
	private String pubdate = null;
	private int itemcount = 0;
	private List<RSSItem> itemlist;
	private boolean sortedNotEmpty = false;
	private static Date firstDate = null;
	private TreeMap<Date, RSSItem> sortedItemList = null;
	private Date savedDate = null;

	public RSSFeed()
	{
		itemlist = new Vector<RSSItem> (0);
		sortedItemList = new TreeMap<Date, RSSItem>();
		setSortedNotEmpty(false);
		savedDate = new Date();
	}
	public int addItem(RSSItem item)
	{
		itemlist.add(item);
		itemcount++;
		return itemcount;
	}
	public RSSItem getItem(int location)
	{
		return itemlist.get(location);
	}
	public List<RSSItem>  getAllItems()
	{
		return itemlist;
	}
	
	public void fillSortedItemList(){
		//Trie des Dates par ordre chronologique grace à la treeMap.
		for(int i=0;i<itemlist.size()-1;i++){
			try {
				sortedItemList.put(stringToDate(itemlist.get(i).getDate().toString()),
						itemlist.get(i));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				try {
					if(!itemlist.get(i).getDate().toString().equals(""))
					sortedItemList.put(stringToDate(itemlist.get(i).getDate().toString() + " 00:00"),
							itemlist.get(i));
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
					e1.printStackTrace();
				}
//				System.out.println("Activité qui plante:"+  itemlist.get(i).getTitle());
			}
		}
	}

	public List<Map<String, Object>>  fillItemList(){
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

		if(!sortedItemList.isEmpty()){
			Date nextActivity = sortedItemList.firstKey();
			Date now = new Date();
			//System.out.println(now.toString());
			if(nextActivity.after(now)){
				//test de l'alarme.
				//now.setMinutes(now.getMinutes()+1);
				setFirstDate(nextActivity);
			}
			else{
				try {
					if(itemlist.size() >= 2)
						setFirstDate(stringToDate(itemlist.get(1).getDate()));
					else{
						System.out.println("RSSFeed: Error with alarm ressources\n");
					}
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		
		itemlist.clear();
		for(Date date : sortedItemList.keySet()){
			HashMap<String, Object>	item = new HashMap<String, Object>();
			item.put(RSSItem.TITLE, sortedItemList.get(date).getTitle());
			item.put(RSSItem.DATE, dateToString(date) + " - " + sortedItemList.get(date).getPeriode());
			data.add(item);
			itemlist.add(sortedItemList.get(date));
		}
			sortedNotEmpty = true;
//			System.out.println("Alarme à " + getFirstDate().toString());
		}
		return data;
	}
	
	public List<Map<String, Object>> getAllItemsForListView(){	
		fillSortedItemList();	
		List<Map<String, Object>> data = fillItemList();
		return data;
	}
	int getItemCount()
	{
		return itemcount;
	}
	public void setTitle(String title)
	{
		this.title = title;
	}
	public void setPubDate(String pubdate)
	{
		this.pubdate = pubdate;
	}
	public String getTitleRSS()
	{
		return title;
	}
	public String getPubDate()
	{
		return pubdate;
	}
	public static SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm");
	/**
	 * Transformation de string en date
	 * @param sDate : le string source
	 * @return : la date resultante
	 * @throws ParseException : en cas de mauvais format du string source.
	 */
	public static Date stringToDate(String sDate) throws ParseException {
		return formatter.parse(sDate);
	}
	/**
	 * Transformation de date en string
	 * @param date : la date source
	 * @return : le string resultante
	 */
	public static String dateToString(Date date) {
		return formatter.format(date);

	}
	/**
	 * @return the firstDate
	 */
	public static Date getFirstDate() {
		return firstDate;
	}
	/**
	 * @param firstDate the firstDate to set
	 */
	public void setFirstDate(Date firstDate) {
		RSSFeed.firstDate = firstDate;
	}
	public boolean isSortedNotEmpty() {
		return sortedNotEmpty;
	}
	public void setSortedNotEmpty(boolean sortedNotEmpty) {
		this.sortedNotEmpty = sortedNotEmpty;
	}
	/**
	 * @return the savedDate
	 */
	public Date getSavedDate() {
		return savedDate;
	}
	/**
	 * @param savedDate the savedDate to set
	 */
	public void setSavedDate(Date savedDate) {
		this.savedDate = savedDate;
	}
	public TreeMap<Date, RSSItem> getSortedItemList() {
		return sortedItemList;
	}
	public void setSortedItemList(TreeMap<Date, RSSItem> sortedItemList) {
		this.sortedItemList = sortedItemList;
	}
}
